using Autofac;
using Autofac.Core.Lifetime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Lcygcc.Infrastructure.Utility.IOC
{
	public class ContainerManager
	{
		private readonly IContainer _container;
		public virtual IContainer Container
		{
			get
			{
				return this._container;
			}
		}
		public ContainerManager(IContainer container)
		{
			this._container = container;
		}
		public virtual T ResolveByName<T>(string name, ILifetimeScope scope = null)
		{
			if (scope == null)
			{
				scope = this.Scope();
			}
			if (scope.IsRegistered<T>())
			{
				return scope.ResolveNamed<T>(name);
			}
			throw new Exception(string.Format("{0} 未注册", name));
		}
		public virtual T Resolve<T>(string key = "", ILifetimeScope scope = null) where T : class
		{
			if (scope == null)
			{
				scope = this.Scope();
			}
			T result;
			if (string.IsNullOrEmpty(key))
			{
				result = scope.Resolve<T>();
			}
			else
			{
				result = scope.ResolveKeyed<T>(key);
			}
			return result;
		}
		public virtual object Resolve(Type type, ILifetimeScope scope = null)
		{
			if (scope == null)
			{
				scope = this.Scope();
			}
			return scope.Resolve(type);
		}
		public virtual T[] ResolveAll<T>(string key = "", ILifetimeScope scope = null)
		{
			if (scope == null)
			{
				scope = this.Scope();
			}
			T[] result;
			if (string.IsNullOrEmpty(key))
			{
				result = scope.Resolve<IEnumerable<T>>().ToArray<T>();
			}
			else
			{
				result = scope.ResolveKeyed<IEnumerable<T>>(key).ToArray<T>();
			}
			return result;
		}
		public virtual T ResolveUnregistered<T>(ILifetimeScope scope = null) where T : class
		{
			return this.ResolveUnregistered(typeof(T), scope) as T;
		}
		public virtual object ResolveUnregistered(Type type, ILifetimeScope scope = null)
		{
			if (scope == null)
			{
				scope = this.Scope();
			}
			ConstructorInfo[] constructors = type.GetConstructors();
			ConstructorInfo[] array = constructors;
			int i = 0;
			object result;
			while (i < array.Length)
			{
				ConstructorInfo constructorInfo = array[i];
				try
				{
					ParameterInfo[] parameters = constructorInfo.GetParameters();
					List<object> list = new List<object>();
					ParameterInfo[] array2 = parameters;
					for (int j = 0; j < array2.Length; j++)
					{
						ParameterInfo parameterInfo = array2[j];
						object obj = this.Resolve(parameterInfo.ParameterType, scope);
						if (obj == null)
						{
							result = null;
							return result;
						}
						list.Add(obj);
					}
					result = Activator.CreateInstance(type, list.ToArray());
					return result;
				}
				catch (Exception ex)
				{
					result = null;
					return result;
				}
			}
			result = null;
			return result;
		}
		public virtual bool TryResolve(Type serviceType, ILifetimeScope scope, out object instance)
		{
			if (scope == null)
			{
				scope = this.Scope();
			}
			return scope.TryResolve(serviceType, out instance);
		}
		public virtual bool IsRegistered(Type serviceType, ILifetimeScope scope = null)
		{
			if (scope == null)
			{
				scope = this.Scope();
			}
			return scope.IsRegistered(serviceType);
		}
		public virtual bool IsRegistered(string name, Type serviceType, ILifetimeScope scope = null)
		{
			if (scope == null)
			{
				scope = this.Scope();
			}
			return scope.IsRegisteredWithName(name, serviceType);
		}
		public virtual object ResolveOptional(Type serviceType, ILifetimeScope scope = null)
		{
			if (scope == null)
			{
				scope = this.Scope();
			}
			return scope.ResolveOptional(serviceType);
		}
		public virtual ILifetimeScope Scope()
		{
			ILifetimeScope result;
			try
			{
				result = this.Container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);
			}
			catch (Exception)
			{
				result = this.Container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);
			}
			return result;
		}
	}
}
